package com.autotest.nore.controller;

import com.autotest.nore.common.utils.PageUtils;
import com.autotest.nore.common.utils.Pager;
import com.autotest.nore.dto.ApiCaseDTO;
import com.autotest.nore.dto.CaseApiDTO;
import com.autotest.nore.dto.CaseApiReportDTO;
import com.autotest.nore.request.ApiCaseRequest;
import com.autotest.nore.request.QueryRequest;
import com.autotest.nore.service.CaseApiService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@RequestMapping("/autotest/caseapi")
public class CaseApiController {

    @Autowired
    private CaseApiService caseApiService;

    @GetMapping("/list")
    public List<ApiCaseDTO> getCaseApiList(@RequestParam String apiId){
        return caseApiService.getCaseApiListByApi(apiId);
    }
    @PostMapping("/lists/{goPage}/{pageSize}")
    public Pager<List<ApiCaseDTO>> getCaseApiList(@PathVariable int goPage, @PathVariable int pageSize,@RequestBody QueryRequest request){
        Page<Object> page = PageHelper.startPage(goPage, pageSize, true);
        return PageUtils.setPageInfo(page,caseApiService.getCaseApiList(request));
    }

    @PostMapping("/save")
    public String saveCaseApi(@RequestBody ApiCaseRequest apiCaseRequest){
         return caseApiService.saveCaseApi(apiCaseRequest);
    }
    @PostMapping("/copy")
    public String copyCaseApi(@RequestBody ApiCaseRequest apiCaseRequest){
        String originalCaseId=apiCaseRequest.getId();
        return caseApiService.copyCaseApi(originalCaseId);
    }

    @GetMapping("/delete/{id}")
    public void delete(@PathVariable String id){
        caseApiService.deleteById(id);
    }


    @GetMapping("/detail/{caseApiId}")
    public ApiCaseDTO getCaseApiDetail(@PathVariable String caseApiId){
        return caseApiService.getCaseApiDetailByCaseApiId(caseApiId);
    }

    @PostMapping("/report/{goPage}/{pageSize}")
    public Pager<List<CaseApiReportDTO>> report(@PathVariable int goPage, @PathVariable int pageSize,
                                                @RequestBody QueryRequest request){
        Page<Object> page = PageHelper.startPage(goPage, pageSize, true);
        String projectId=request.getProjectId();
        String condition=request.getCondition();
        Boolean existsInCaseApiIndex=request.getExistsInCaseApiIndex();
        return PageUtils.setPageInfo(page,caseApiService.getCaseApiReport(projectId,condition,existsInCaseApiIndex));
    }


}
